package com.dhcc.core.modules.msgcenter.push;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.mail.HtmlEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.dhcc.core.framework.util.properties.GlobalProperties;
import com.dhcc.core.modules.msgcenter.vo.MsgPush;
import com.dhcc.core.modules.system.entity.User;

/**
 * 发送电子邮件
 */
@Service("emailPushSerivce")
public class EmailPushService extends AbsPushService {

    private final static Logger logger = LoggerFactory.getLogger(EmailPushService.class);

    @Override
    public void send(MsgPush msgPush, String pushType) {
        // 自行处理
        List<User> receiveUsers = this.getReceiveUsers(msgPush, pushType);
        for (Iterator<User> iterator = receiveUsers.iterator(); iterator.hasNext();) {
            User user = (User) iterator.next();
            boolean result = send(user.getEmail(), msgPush.getMsgTitle(), msgPush.getMsgContentByTpl());
            if (!result) {
                this.pushError(msgPush, user, pushType);
            }
        }
    }

    /**
     * 发送邮件
     * 
     * @param toAddress
     *            接收地址
     * @param subject
     *            标题
     * @param content
     *            内容
     * @return
     */
    private boolean send(String toAddress, String subject, String content) {
        GlobalProperties props = GlobalProperties.me();
        String fromAddress = props.getProperty("sys.msg.email.fromAddress");
        String fromPassword = props.getProperty("sys.msg.email.fromPassword");
        String fromHostName = props.getProperty("sys.msg.email.fromHostName");
        String sslOnConnect = props.getProperty("sys.msg.email.sslOnConnect", "false");
        String sslSmtpPort = props.getProperty("sys.msg.email.sslSmtpPort");
        return send(fromAddress, fromPassword, fromHostName, sslOnConnect, sslSmtpPort, toAddress, subject, content);
    }

    /**
     * 发送邮件
     * 
     * @param toAddress
     *            接收地址
     * @param subject
     *            标题
     * @param content
     *            内容
     * @return
     */
    private boolean send(String fromAddress, String fromPassword, String fromHostName, String sslOnConnect,
            String sslSmtpPort, String toAddress, String subject, String content) {
        try {
            HtmlEmail htmlEmail = new HtmlEmail();
            // 发送地址
            htmlEmail.setFrom(fromAddress);
            // 密码校验
            htmlEmail.setAuthentication(fromAddress, fromPassword);
            // 发送服务器协议
            htmlEmail.setHostName(fromHostName);

            // SSL
            if ("true".equals(sslOnConnect)) {
                htmlEmail.setSSLOnConnect(true);
                htmlEmail.setSslSmtpPort(sslSmtpPort);
            }

            // 接收地址
            htmlEmail.addTo(toAddress);

            // 标题
            htmlEmail.setSubject(subject);
            // 内容
            htmlEmail.setMsg(content);

            // 其他信息
            htmlEmail.setCharset("utf-8");

            // 发送
            htmlEmail.send();
            return true;
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
        return false;
    }

}